#####################################################################
# Preparing Figure 1
#####################################################################

rm(list=ls())

library(Hmisc)
library(exactRankTests)
library(ggplot2)
library(reshape2)
library(xtable)
library(stargazer)
library(sensitivitymw)
library(sensitivitymv)
library(lmtest)
library(sandwich)
library(stargazer)
library(msm)

# Read data and prepare basic output
load("~/Dropbox/Crime Brazil/06_replication/001_rematch_crime.Rdata")

#################################################################
# Standardized differences between unmatched and matched sample
#################################################################

# mom 1
num0_mom1 = abs(mean(d$v2_bairrosafe_wave1_imp)- mean(d_match$v2_bairrosafe_wave1_imp))
den0_mom1 = sqrt(  ( (sd(d$v2_bairrosafe_wave1_imp))^2 + (sd(d_match$v2_bairrosafe_wave1_imp))^2 )/2  )
sd0_mom1 = num0_mom1/den0_mom1
sd0_mom1

# mom 2
num0_mom2 = abs(mean(d$v7a_mediatvyesorno_wave3_imp)- mean(d_match$v7a_mediatvyesorno_wave3_imp))
den0_mom2 = sqrt(  ( (sd(d$v7a_mediatvyesorno_wave3_imp))^2 + (sd(d_match$v7a_mediatvyesorno_wave3_imp))^2 )/2  )
sd0_mom2 = num0_mom2/den0_mom2
sd0_mom2

# mom 3
num0_mom3 = abs(mean(d$v9a_mediapaperyesorno_wave3_imp)- mean(d_match$v9a_mediapaperyesorno_wave3_imp))
den0_mom3 = sqrt(  ( (sd(d$v9a_mediapaperyesorno_wave3_imp))^2 + (sd(d_match$v9a_mediapaperyesorno_wave3_imp))^2 )/2  )
sd0_mom3 = num0_mom3/den0_mom3
sd0_mom3

# mom 4
num0_mom4 = abs(mean(d$v10c_mediainternetfreq_wave3_imp)- mean(d_match$v10c_mediainternetfreq_wave3_imp))
den0_mom4 = sqrt(  ( (sd(d$v10c_mediainternetfreq_wave3_imp))^2 + (sd(d_match$v10c_mediainternetfreq_wave3_imp))^2 )/2  )
sd0_mom4 = num0_mom4/den0_mom4
sd0_mom4

# mom 5
num0_mom5 = abs(mean(d$v16e_conversefriends_wave3_imp)- mean(d_match$v16e_conversefriends_wave3_imp))
den0_mom5 = sqrt(  ( (sd(d$v16e_conversefriends_wave3_imp))^2 + (sd(d_match$v16e_conversefriends_wave3_imp))^2 )/2  )
sd0_mom5 = num0_mom5/den0_mom5
sd0_mom5

# mom 6
num0_mom6 = abs(mean(d$v16g_conversefamily_wave3_imp)- mean(d_match$v16g_conversefamily_wave3_imp))
den0_mom6 = sqrt(  ( (sd(d$v16g_conversefamily_wave3_imp))^2 + (sd(d_match$v16g_conversefamily_wave3_imp))^2 )/2  )
sd0_mom6 = num0_mom6/den0_mom6
sd0_mom6

# mom 7
num0_mom7 = abs(mean(d$v20_comparetobairro_wave3_imp)- mean(d_match$v20_comparetobairro_wave3_imp))
den0_mom7 = sqrt(  ( (sd(d$v20_comparetobairro_wave3_imp))^2 + (sd(d_match$v20_comparetobairro_wave3_imp))^2 )/2  )
sd0_mom7 = num0_mom7/den0_mom7
sd0_mom7

# mom 8
num0_mom8 = abs(mean(d$v30a_combat_crime_wave1)- mean(d_match$v30a_combat_crime_wave1))
den0_mom8 = sqrt(  ( (sd(d$v30a_combat_crime_wave1))^2 + (sd(d_match$v30a_combat_crime_wave1))^2 )/2  )
sd0_mom8 = num0_mom8/den0_mom8
sd0_mom8

# mom 9
num0_mom9 = abs(mean(d$v35a_attentionpres_wave2_imp)- mean(d_match$v35a_attentionpres_wave2_imp))
den0_mom9 = sqrt(  ( (sd(d$v35a_attentionpres_wave2_imp))^2 + (sd(d_match$v35a_attentionpres_wave2_imp))^2 )/2  )
sd0_mom9 = num0_mom9/den0_mom9
sd0_mom9

# mom 11
num0_mom10 = abs(mean(d$v36a_persuadefromothersyesorno_wave3_imp)- mean(d_match$v36a_persuadefromothersyesorno_wave3_imp))
den0_mom10 = sqrt(  ( (sd(d$v36a_persuadefromothersyesorno_wave3_imp))^2 + (sd(d_match$v36a_persuadefromothersyesorno_wave3_imp))^2 )/2  )
sd0_mom10 = num0_mom10/den0_mom10
sd0_mom10

# mom 11
num0_mom11 = abs(mean(d$v40a_thermmilitary_wave1_imp)- mean(d_match$v40a_thermmilitary_wave1_imp))
den0_mom11 = sqrt(  ( (sd(d$v40a_thermmilitary_wave1_imp))^2 + (sd(d_match$v40a_thermmilitary_wave1_imp))^2 )/2  )
sd0_mom11 = num0_mom11/den0_mom11
sd0_mom11

# mom 12
num0_mom12 = abs(mean(d$v40b_thermCUT_wave1_imp)- mean(d_match$v40b_thermCUT_wave1_imp))
den0_mom12 = sqrt(  ( (sd(d$v40b_thermCUT_wave1_imp))^2 + (sd(d_match$v40b_thermCUT_wave1_imp))^2 )/2  )
sd0_mom12 = num0_mom12/den0_mom12
sd0_mom12

# mom 13
num0_mom13 = abs(mean(d$v40d_thermbusiness_wave1_imp)- mean(d_match$v40d_thermbusiness_wave1_imp))
den0_mom13 = sqrt(  ( (sd(d$v40d_thermbusiness_wave1_imp))^2 + (sd(d_match$v40d_thermbusiness_wave1_imp))^2 )/2  )
sd0_mom13 = num0_mom13/den0_mom13
sd0_mom13

# mom 14
num0_mom14 = abs(mean(d$v41b_thermfhc_wave3_imp)- mean(d_match$v41b_thermfhc_wave3_imp))
den0_mom14 = sqrt(  ( (sd(d$v41b_thermfhc_wave3_imp))^2 + (sd(d_match$v41b_thermfhc_wave3_imp))^2 )/2  )
sd0_mom14 = num0_mom14/den0_mom14
sd0_mom14

# mom 15
num0_mom15 = abs(mean(d$v43a_pidyesorno_wave3)- mean(d_match$v43a_pidyesorno_wave3))
den0_mom15 = sqrt(  ( (sd(d$v43a_pidyesorno_wave3))^2 + (sd(d_match$v43a_pidyesorno_wave3))^2 )/2  )
sd0_mom15 = num0_mom15/den0_mom15
sd0_mom15

# mom 16
num0_mom16 = abs(mean(d$v46_pidimportant_wave1_imp)- mean(d_match$v46_pidimportant_wave1_imp))
den0_mom16 = sqrt(  ( (sd(d$v46_pidimportant_wave1_imp))^2 + (sd(d_match$v46_pidimportant_wave1_imp))^2 )/2  )
sd0_mom16 = num0_mom16/den0_mom16
sd0_mom16

# mom 17
num0_mom17 = abs(mean(d$v50_ideology_wave3_imp)- mean(d_match$v50_ideology_wave3_imp))
den0_mom17 = sqrt(  ( (sd(d$v50_ideology_wave3_imp))^2 + (sd(d_match$v50_ideology_wave3_imp))^2 )/2  )
sd0_mom17 = num0_mom17/den0_mom17
sd0_mom17

# mom 18
num0_mom18 = abs(mean(d$v69a_issuessocialspend_wave3_imp)- mean(d_match$v69a_issuessocialspend_wave3_imp))
den0_mom18 = sqrt(  ( (sd(d$v69a_issuessocialspend_wave3_imp))^2 + (sd(d_match$v69a_issuessocialspend_wave3_imp))^2 )/2  )
sd0_mom18 = num0_mom18/den0_mom18
sd0_mom18

# mom 19
num0_mom19 = abs(mean(d$v72b_issuesrendaminima_wave3_imp)- mean(d_match$v72b_issuesrendaminima_wave3_imp))
den0_mom19 = sqrt(  ( (sd(d$v72b_issuesrendaminima_wave3_imp))^2 + (sd(d_match$v72b_issuesrendaminima_wave3_imp))^2 )/2  )
sd0_mom19 = num0_mom19/den0_mom19
sd0_mom19

# mom 20
num0_mom20 = abs(mean(d$s1_sex_wave3)- mean(d_match$s1_sex_wave3))
den0_mom20 = sqrt(  ( (sd(d$s1_sex_wave3))^2 + (sd(d_match$s1_sex_wave3))^2 )/2  )
sd0_mom20= num0_mom20/den0_mom20
sd0_mom20

# mom 21
num0_mom21 = abs(mean(d$s6_education_wave2_imp)- mean(d_match$s6_education_wave2_imp))
den0_mom21 = sqrt(  ( (sd(d$s6_education_wave2_imp))^2 + (sd(d_match$s6_education_wave2_imp))^2 )/2  )
sd0_mom21 = num0_mom21/den0_mom21
sd0_mom21

# mom 22
num0_mom22 = abs(mean(d$s7a_jobfixed_wave3_imp)- mean(d_match$s7a_jobfixed_wave3_imp))
den0_mom22 = sqrt(  ( (sd(d$s7a_jobfixed_wave3_imp))^2 + (sd(d_match$s7a_jobfixed_wave3_imp))^2 )/2  )
sd0_mom22 = num0_mom22/den0_mom22
sd0_mom22

# mom 23
num0_mom23 = abs(mean(d$s7b_jobformalsector_wave1_imp)- mean(d_match$s7b_jobformalsector_wave1_imp))
den0_mom23 = sqrt(  ( (sd(d$s7b_jobformalsector_wave1_imp))^2 + (sd(d_match$s7b_jobformalsector_wave1_imp))^2 )/2  )
sd0_mom23 = num0_mom23/den0_mom23
sd0_mom23

# mom 24
num0_mom24 = abs(mean(d$s7c_jobpublicsector_wave1_imp)- mean(d_match$s7c_jobpublicsector_wave1_imp))
den0_mom24 = sqrt(  ( (sd(d$s7c_jobpublicsector_wave1_imp))^2 + (sd(d_match$s7c_jobpublicsector_wave1_imp))^2 )/2  )
sd0_mom24 = num0_mom24/den0_mom24
sd0_mom24

# mom 25
num0_mom25 = abs(mean(d$s7e_jobworry_wave3_imp)- mean(d_match$s7e_jobworry_wave3_imp))
den0_mom25 = sqrt(  ( (sd(d$s7e_jobworry_wave3_imp))^2 + (sd(d_match$s7e_jobworry_wave3_imp))^2 )/2  )
sd0_mom25 = num0_mom25/den0_mom25
sd0_mom25

# mom 26
num0_mom26 = abs(mean(d$s10_age_wave3_imp)- mean(d_match$s10_age_wave3_imp))
den0_mom26 = sqrt(  ( (sd(d$s10_age_wave3_imp))^2 + (sd(d_match$s10_age_wave3_imp))^2 )/2  )
sd0_mom26 = num0_mom26/den0_mom26
sd0_mom26

# mom 27
num0_mom27 = abs(mean(d$s14c_know3fhcparty_wave3)- mean(d_match$s14c_know3fhcparty_wave3))
den0_mom27 = sqrt(  ( (sd(d$s14c_know3fhcparty_wave3))^2 + (sd(d_match$s14c_know3fhcparty_wave3))^2 )/2  )
sd0_mom27 = num0_mom27/den0_mom27
sd0_mom27

# mom 28
num0_mom28 = abs(mean(d$crime_stronghand_wave1)- mean(d_match$crime_stronghand_wave1))
den0_mom28 = sqrt(  ( (sd(d$crime_stronghand_wave1))^2 + (sd(d_match$crime_stronghand_wave1))^2 )/2  )
sd0_mom28 = num0_mom28/den0_mom28
sd0_mom28

# mom 29
num0_mom29 = abs(mean(d$deathpenalty_reducecrime_wave1)- mean(d_match$deathpenalty_reducecrime_wave1))
den0_mom29 = sqrt(  ( (sd(d$deathpenalty_reducecrime_wave1))^2 + (sd(d_match$deathpenalty_reducecrime_wave1))^2 )/2  )
sd0_mom29 = num0_mom29/den0_mom29
sd0_mom29

# mom 30
num0_mom30 = abs(mean(d$supportdem_wave1)- mean(d_match$supportdem_wave1))
den0_mom30 = sqrt(  ( (sd(d$supportdem_wave1))^2 + (sd(d_match$supportdem_wave1))^2 )/2  )
sd0_mom30 = num0_mom30/den0_mom30
sd0_mom30

# mom 31
num0_mom31 = abs(mean(d$deathpenalty_support_wave2)- mean(d_match$deathpenalty_support_wave2))
den0_mom31 = sqrt(  ( (sd(d$deathpenalty_support_wave2))^2 + (sd(d_match$deathpenalty_support_wave2))^2 )/2  )
sd0_mom31 = num0_mom31/den0_mom31
sd0_mom31

# mom 32
num0_mom32 = abs(mean(d$voteforciro_wave3)- mean(d_match$voteforciro_wave3))
den0_mom32 = sqrt(  ( (sd(d$voteforciro_wave3))^2 + (sd(d_match$voteforciro_wave3))^2 )/2  )
sd0_mom32 = num0_mom32/den0_mom32
sd0_mom32

# mom 33
num0_mom33 = abs(mean(d$voteforlula_wave3)- mean(d_match$voteforlula_wave3))
den0_mom33 = sqrt(  ( (sd(d$voteforlula_wave3))^2 + (sd(d_match$voteforlula_wave3))^2 )/2  )
sd0_mom33 = num0_mom33/den0_mom33
sd0_mom33

# mom 34
num0_mom34 = abs(mean(d$voteforserra_wave3)- mean(d_match$voteforserra_wave3))
den0_mom34 = sqrt(  ( (sd(d$voteforserra_wave3))^2 + (sd(d_match$voteforserra_wave3))^2 )/2  )
sd0_mom34 = num0_mom34/den0_mom34
sd0_mom34

# mom 35
num0_mom35 = abs(mean(d$voteforgarotinho_wave3)- mean(d_match$voteforgarotinho_wave3))
den0_mom35 = sqrt(  ( (sd(d$voteforgarotinho_wave3))^2 + (sd(d_match$voteforgarotinho_wave3))^2 )/2  )
sd0_mom35 = num0_mom35/den0_mom35
sd0_mom35

# mom 36
num0_mom36 = abs(mean(d$party_pmdb_wave3)- mean(d_match$party_pmdb_wave3))
den0_mom36 = sqrt(  ( (sd(d$party_pmdb_wave3))^2 + (sd(d_match$party_pmdb_wave3))^2 )/2  )
sd0_mom36 = num0_mom36/den0_mom36
sd0_mom36

# mom 37
num0_mom37 = abs(mean(d$party_pfl_wave3)- mean(d_match$party_pfl_wave3))
den0_mom37 = sqrt(  ( (sd(d$party_pfl_wave3))^2 + (sd(d_match$party_pfl_wave3))^2 )/2  )
sd0_mom37 = num0_mom37/den0_mom37
sd0_mom37

# mom 38
num0_mom38 = abs(mean(d$party_psdb_wave3)- mean(d_match$party_psdb_wave3))
den0_mom38 = sqrt(  ( (sd(d$party_psdb_wave3))^2 + (sd(d_match$party_psdb_wave3))^2 )/2  )
sd0_mom38 = num0_mom38/den0_mom38
sd0_mom38

# mom 39
num0_mom39 = abs(mean(d$party_pt_wave3)- mean(d_match$party_pt_wave3))
den0_mom39 = sqrt(  ( (sd(d$party_pt_wave3))^2 + (sd(d_match$party_pt_wave3))^2 )/2  )
sd0_mom39 = num0_mom39/den0_mom39
sd0_mom39

# mom 40
num0_mom40 = abs(mean(d$race_white_wave1)- mean(d_match$race_white_wave1))
den0_mom40 = sqrt(  ( (sd(d$race_white_wave1))^2 + (sd(d_match$race_white_wave1))^2 )/2  )
sd0_mom40 = num0_mom40/den0_mom40
sd0_mom40

# mom 41
num0_mom41 = abs(mean(d$race_mestizo_wave1)- mean(d_match$race_mestizo_wave1))
den0_mom41 = sqrt(  ( (sd(d$race_mestizo_wave1))^2 + (sd(d_match$race_mestizo_wave1))^2 )/2  )
sd0_mom41 = num0_mom41/den0_mom41
sd0_mom41

# mom 42
num0_mom42 = abs(mean(d$race_black_wave1)- mean(d_match$race_black_wave1))
den0_mom42 = sqrt(  ( (sd(d$race_black_wave1))^2 + (sd(d_match$race_black_wave1))^2 )/2  )
sd0_mom42 = num0_mom42/den0_mom42
sd0_mom42

# mom 43
num0_mom43 = abs(mean(d$vote98_fhc_wave1)- mean(d_match$vote98_fhc_wave1))
den0_mom43 = sqrt(  ( (sd(d$vote98_fhc_wave1))^2 + (sd(d_match$vote98_fhc_wave1))^2 )/2  )
sd0_mom43 = num0_mom43/den0_mom43
sd0_mom43

# mom 44
num0_mom44 = abs(mean(d$vote98_lula_wave1)- mean(d_match$vote98_lula_wave1))
den0_mom44 = sqrt(  ( (sd(d$vote98_lula_wave1))^2 + (sd(d_match$vote98_lula_wave1))^2 )/2  )
sd0_mom44 = num0_mom44/den0_mom44
sd0_mom44

# mom 45
num0_mom45 = abs(mean(d$religion_catholic_wave1)- mean(d_match$religion_catholic_wave1))
den0_mom45 = sqrt(  ( (sd(d$religion_catholic_wave1))^2 + (sd(d_match$religion_catholic_wave1))^2 )/2  )
sd0_mom45 = num0_mom45/den0_mom45
sd0_mom45

# mom 46
num0_mom46 = abs(mean(d$religion_evangelic_wave1)- mean(d_match$religion_evangelic_wave1))
den0_mom46 = sqrt(  ( (sd(d$religion_evangelic_wave1))^2 + (sd(d_match$religion_evangelic_wave1))^2 )/2  )
sd0_mom46 = num0_mom46/den0_mom46
sd0_mom46

# mom 47
num0_mom47 = abs(mean(d$noreligion_wave1)- mean(d_match$noreligion_wave1))
den0_mom47 = sqrt(  ( (sd(d$noreligion_wave1))^2 + (sd(d_match$noreligion_wave1))^2 )/2  )
sd0_mom47 = num0_mom47/den0_mom47
sd0_mom47

##################
# Generate data
##################

colnames = c("Perceptions of safety", #mom1
             "Do you watch TV news?", #mom2
             "Do you read about politics in newspapers", #mom3
             "Frequency of internet usage", #mom4
             "Talk about politics with friends", #mom5
             "Talk about politics with family", #mom6
             "Comparison with other families", #mom7
             "Importance of combating crime", #mom8
             "Attention paid to presidential election", #mom9
             "Have you persuaded others to vote?", #mom10
             "Military feeling thermometer", #mom11
             "Union feeling thermometer", #mom12
             "Business sector feeling thermometer", #mom13
             "President (FHC) feeling thermometer", #mom14
             "Do you identify with a party?", #mom15
             "Importance of party when you vote", #mom16
             "Ideology", #mom17
             "Opinions about social spending", #mom18
             "Opinions about minimum wage", #mom19
             "Gender", #mom20
             "Education", #mom21
             "Stable job", #mom22
             "Job in the formal sector", #mom23
             "Job in the public sector", #mom24
             "Worried about losing job in the future", #mom25
             "Age", #mom26
             "Name of one presidential candidate", #mom27
             "Support strong-arm policies to reduce crime", #mom28
             "Support death penalty to reduce crime", #mom29
             "Support for democracy", #mom30
             "Support for death penalty", #mom31
             "Vote for Ciro", #mom32
             "Vote for Lula", #mom33
             "Vote for Serra", #mom34
             "Vote for Garotinho", #mom35
             "Do you identify with the PMDB", #mom36
             "Do you identify with the PFL", #mom37
             "Do you identify with the PSDB", #mom38
             "Do you identify with the PT", #mom39
             "White", #mom40
             "Pardo/Mestizo", #mom41
             "Black", #mom42
             "Voted for FHC in 1998", #mom43
             "Voted for Lula in 1998", #mom44
             "Catholic", #mom45
             "Evangelical", #mom46
             "No religion") #mom47

sd0 =  c(sd0_mom1,
         sd0_mom2,
         sd0_mom3,
         sd0_mom4,
         sd0_mom5,
         sd0_mom6,
         sd0_mom7,
         sd0_mom8,
         sd0_mom9,
         sd0_mom10,
         sd0_mom11,
         sd0_mom12,
         sd0_mom13,
         sd0_mom14,
         sd0_mom15,
         sd0_mom16,
         sd0_mom17,
         sd0_mom18,
         sd0_mom19,
         sd0_mom20,
         sd0_mom21,
         sd0_mom22,
         sd0_mom23,
         sd0_mom24,
         sd0_mom25,
         sd0_mom26,
         sd0_mom27,
         sd0_mom28,
         sd0_mom29,
         sd0_mom30,
         sd0_mom31,
         sd0_mom32,
         sd0_mom33,
         sd0_mom34,
         sd0_mom35,
         sd0_mom36,
         sd0_mom37,
         sd0_mom38,
         sd0_mom39,
         sd0_mom40,
         sd0_mom41,
         sd0_mom42,
         sd0_mom43,
         sd0_mom44,
         sd0_mom45,
         sd0_mom46,
         sd0_mom47)

Comparison = rep("Matched sample-Umatched sample",47)

meanbalance0 = data.frame(colnames,sd0,Comparison)
meanbalance0$order <- nrow(meanbalance0):1
meanbalance0 <-meanbalance0[order(meanbalance0$order, decreasing=FALSE),]
meanbalance0$colnames <- factor(meanbalance0$colnames, levels = meanbalance0$colnames[order(meanbalance0$order)])
meanbalance0
names(meanbalance0)

print(levels(meanbalance0$colnames))

colnames(meanbalance0) = c("covariate","sd","Comparison","order")
head(meanbalance0,47)

save(meanbalance0, file="~/Dropbox/Crime Brazil/06_replication/002_representativeness.Rdata")
